package com.wyait.manage.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyait.manage.pojo.User;
import com.wyait.manage.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Package: com.crb.crm.manage.utils
 * Description：
 * Author: huangjian
 * Date:  2020/9/11 12:25
 * Modified By:
 *
 * @author huangjian
 */
public class SpringSecurityUtils {

    public static UserService authService;
    private static final Logger logger = LoggerFactory.getLogger(SpringSecurityUtils.class);
    private static final ObjectMapper mapper = new ObjectMapper();

    public static User currentUser() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String authenticationName = request.getHeader("X-Auth-Name");
        logger.info("当前用户：" + authenticationName);
        if (StringUtils.isEmpty(authenticationName)) {
            return null;
        }
        return authService.findUserByUsername(authenticationName);
    }
}
